package test;

import core.buffer.Buffer;
import core.net.Connection;
import core.net.NetConfig;
import core.net.TCPClient;
import core.net.TCPServer;

import java.util.ArrayList;
import java.util.List;

public class NetCApp {

    public static final List<Connection> connections = new ArrayList<>();

    public static void main(String[] args) throws InterruptedException {
        NetConfig config = new NetConfig(5000, 32, 10 * 1000);
        TCPClient client = new TCPClient("127.0.0.1", 32145, config);
        client.connectedHandler(connection -> {
            synchronized (connections) {
                connections.add(connection);
                connection.open();
            }
        });
        client.open();
        long time;
        long use;
        long del = 500;
        Buffer buffer = new Buffer(5);
        while (true) {
            time = System.currentTimeMillis();
            synchronized (connections) {
                for (int i = 0; i < connections.size(); i++) {
                    Connection connection = connections.get(i);
                    buffer.clear();
                    buffer.write((byte) 4);
                    buffer.writeInt(12);
                    connection.write(buffer);
                }
            }
            use = System.currentTimeMillis() - time;
            Thread.sleep(del - use);
        }
    }
}
